﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace NB_Controller_Test
{
    public partial class MainUIEx : UserControl
    {
        //NB_ProtocolManager nb_ProlManger = new NB_ProtocolManager();

        public bool SendOnOffCmd(bool bOn, IntPtr intPtr)
        {
            string strJSon = "";
            string strOn = "{\"reqId\": 20,\"op\": \"W\",\"prop\": {\"switch\":1}}";
            string strOff = "{\"reqId\": 21,\"op\": \"W\",\"prop\": {\"switch\":0}}";
            if (bOn)
            {
                strJSon = strOn;
            }
            else
            {
                strJSon = strOff;
            }

            return SendMsgNeedJSonBody(strJSon, intPtr);
        }

        public bool SendRequreVerCmd(IntPtr intPtr)
        {
            string strJSon = "{\"reqId\": 22,\"op\": \"R\",\"prop\": [\"fwv\"]}";

            return  SendMsgNeedJSonBody(strJSon, intPtr);
        }

        public bool SendResetCmd(IntPtr intPtr)
        {
            string strJSon = "{\"reqId\": 23,\"cmdId\": \"Reboot\"}";

            return SendMsgNeedJSonBody(strJSon, intPtr, "0x02");
        }

        public bool SendGetWifiInfoCmd(IntPtr intPtr)
        {
            string strJSon = "{\"reqId\": 24,\"op\": \"R\",\"prop\": [\"wifiInfo\"]}";

            return SendMsgNeedJSonBody(strJSon, intPtr);
        }

        public bool SendGetDevInfoCmd(IntPtr intPtr)
        {
            string strJSon = "{\"reqId\": 25,\"op\": \"R\",\"prop\": [\"wifiInfo\",\"nbBase\",\"fwv\",\"loc\"]}";

            return SendMsgNeedJSonBody(strJSon, intPtr);
        }

        public bool SendGetDevFWVInfoCmd(IntPtr intPtr)
        {
            string strJSon = "{\"reqId\": 25,\"op\": \"R\",\"prop\": [\"fwv\"]}";

            bool bOK = SendMsgNeedJSonBody(strJSon, intPtr);

            return bOK;
        }

        public bool SendGetDevClasSkuFWVInfoCmd(IntPtr intPtr)
        {
            string strJSon = "{\"reqId\": 25,\"op\": \"R\",\"prop\": [\"fwv\",\"clas\",\"sku\"]}";

            bool bOK = SendMsgNeedJSonBody(strJSon, intPtr);

            //ASku主动问，因为有些设备不支持此属性，若一起读取将都不返回
            bOK &= SendGetASkuCmd(IntPtr.Zero);

            return bOK;
        }

        public bool SendGetDevInfo_Clas_SkuCmd(IntPtr intPtr)
        {
            //return SendGetDevInfoCmd(intPtr);

            //ASku主动问，因为有些设备不支持此属性，若一起读取将都不返回
            //SendGetASkuCmd(IntPtr.Zero);
            string strJSon = "{\"reqId\": 25,\"op\": \"R\",\"prop\": [\"wifiInfo\",\"nbBase\",\"fwv\",\"loc\",\"clas\",\"sku\"]}";

            bool bOK = SendMsgNeedJSonBody(strJSon, intPtr);

            //ASku主动问，因为有些设备不支持此属性，若一起读取将都不返回
            bOK &= SendGetASkuCmd(IntPtr.Zero);

            return bOK;
        }

        public bool SendGetASkuCmd(IntPtr intPtr)
        {
            string strJSon = "{\"reqId\": 25,\"op\": \"R\",\"prop\": [\"asku\"]}";

            return SendMsgNeedJSonBody(strJSon, intPtr);
        }

        public bool SendOTACmd(string strHfsIP, string strPort, string strBinFile, IntPtr intPtr)
        {
            //string strScan = "{\"reqId\": 41,\"cmdId\": \"OTAUDP\",\"cmdData\": {\"type\":1,\"url\": \"" + strHfsIP + "\",\"port\":80,\"version\":\"/" + strBinFile + "\",\"size\":200}}";
            string strScan = "{\"reqId\": 41,\"cmdId\": \"OTAUDP\",\"cmdData\": {\"type\":1,\"url\": \"" + strHfsIP + "\",\"port\":" + strPort + ",\"version\":\"/" + strBinFile + "\",\"size\":200}}";

            //IntPtr intPrt = IntPtr.Zero;
            //ComboBoxItem itemSel = devIP.SelectedItem as ComboBoxItem;
            //if (itemSel != null)
            //{
            //    intPrt = (IntPtr)(itemSel.Tag);
            //}

            return SendMsgNeedJSonBody(strScan, intPtr, "0x02");
        }

        public bool SendOTACmd_Ex(string strHfsIP, string strPort, string strBinFile, IntPtr intPtr)
        {
            /*
             * 
             * 若Clas或Sku不符合，返回
             * BE BE BE BE 
                01 00 00 49 00 00 00 1B 00 0D 01 02 
                {
                  "reqId": 41,
                  "err": 513,
                  "errDesc": "OTA clas or sku error",
                  "cmdId": "OTAUDP"
                }
                ED ED
             * */
            //2019-7-22 增加下发OTA命令时带上clas sku 等相关信息
            //选择的Bin文件也依照clas_sku_ver.bin命名，加载时需根据规则解析，符合才下发给设备，否则不下发
            //根据协议，删除type和size字段
            //根据协议，下发的clas和sku只从文件名称中读取解析，不跟设备端读取回来的做比对
            string strBinFileName = strBinFile.Substring(0, strBinFile.IndexOf("."));
            string[] strClasSku = strBinFileName.Split('_');
            if(strClasSku.Length >= 3)
            {
                string strClas = strClasSku[0];
                string strSku = strClasSku[1];
                string strScan = "{\"reqId\": 41,\"cmdId\": \"OTAUDP\",\"cmdData\": {\"clas\": \"" + strClas + "\",\"sku\":\"" + strSku + "\",\"url\": \"" + strHfsIP + "\",\"port\":" + strPort + ",\"version\":\"/" + strBinFile + "\",\"size\":200}}";

                //IntPtr intPrt = IntPtr.Zero;
                //ComboBoxItem itemSel = devIP.SelectedItem as ComboBoxItem;
                //if (itemSel != null)
                //{
                //    intPrt = (IntPtr)(itemSel.Tag);
                //}

                return SendMsgNeedJSonBody(strScan, intPtr, "0x02");
            }
            else
            {
                return false;
            }            
        }

        public bool SendCheckOTAState(IntPtr intPtr)
        {
            string strScan = "{\"reqId\": 42,\"cmdId\": \"OTAUDP\"}";

            return SendMsgNeedJSonBody(strScan, intPtr, "0x02");
        }

        //private UInt32 _ulSeqNum = 0x00001234;
        //private bool SendMsgNeedJSonBody(string strJSon, IntPtr intPtr, string strContentType = "0x01")
        //{
        //    NB_Protocol nbProl = new NB_Protocol();
        //    nbProl.byProlID = GlobalFun.HexStringToByte(tbProlID.Text);
        //    nbProl.byProlVer = GlobalFun.HexStringToByte(tbProlVer.Text);

        //    nbProl.nbProlPayload.ulSeqNum = _ulSeqNum++;
        //    nbProl.nbProlPayload.byIsRsp = GlobalFun.HexStringToByte(tbIsRsp.Text);
        //    //nbProl.nbProlPayload.byContentType = GlobalFun.HexStringToByte(cbContentType.Text);
        //    nbProl.nbProlPayload.byContentType = GlobalFun.HexStringToByte(strContentType);
        //    //string strScan = "{\"reqId\": 20,\"op\": \"R\",\"prop\": [\"fwv\"]}";
        //    nbProl.nbProlPayload.strContentData = strJSon;

        //    return SendMsgByNB_Protocol(nbProl, intPtr);
        //}

        public UInt16 _uiSeqNum = 0x0001;
        private bool SendMsgNeedJSonBody(string strJSon, IntPtr intPtr, string strContentType = "0x01")
        {
            NB_Protocol nbProl = new NB_Protocol();
            nbProl.byProlID = GlobalFun.HexStringToByte(tbProlID.Text);
            nbProl.byProlVer = GlobalFun.HexStringToByte(tbProlVer.Text);

            //获取当前IP
            string strIP = srcIP.Text;
            strIP = strIP.Substring(strIP.LastIndexOf('.') + 1);
            UInt16 uiIP = GlobalFun.StringToUInt16(strIP);
            //组合成SeqNum
            UInt32 unSeqNum = (UInt32)((uiIP << 16) + _uiSeqNum);
            nbProl.nbProlPayload.ulSeqNum = unSeqNum;
            //加1
            _uiSeqNum++;
            if (_uiSeqNum >= UInt16.MaxValue)
            {
                _uiSeqNum = 0x0001;
            }

            nbProl.nbProlPayload.byIsRsp = GlobalFun.HexStringToByte(tbIsRsp.Text);
            nbProl.nbProlPayload.byContentType = GlobalFun.HexStringToByte(strContentType);
            nbProl.nbProlPayload.strContentData = strJSon;

            return SendMsgByNB_Protocol(nbProl, intPtr);
        }

        private bool SendMsgByNB_Protocol(NB_Protocol nbProl, IntPtr intPtr)
        {
            Byte[] byMsgData = nbProl.GetProlMsgData();
            if (byMsgData == null)
            {
                System.Diagnostics.Debug.WriteLine("byMsgData == null");
                return false;
            }
            if (byMsgData.Length > 0x05c0)
            {
                System.Diagnostics.Debug.WriteLine("byMsgData.Length > 0x05c0");
                System.Windows.MessageBox.Show("UDP包数据量太大，超过了0x05c0大小限制！");
                return false;
            }

            udpDataRecv.AddSendMsg(byMsgData, byMsgData.Length, intPtr);
            return true;
        }       
    }
}
